#pragma once


#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>

template<class T>
using func_t = std::function<void(T)>;

template<class T>
class Thread
{
public:
    Thread(const std::string &name, func_t<T> func, T data)
           :_tid(0),_threadname(name),_func(func),_data(data),isrunning(false)
    {}
    static void *rountine(void *args)
    {
        Thread *ts = static_cast<Thread*>(args);
        ts->_func(ts->_data);
        return NULL;
    }
    bool Start()
    {
        int n = pthread_create(&_tid, NULL, rountine, this);
        if(n == 0)
        {
            isrunning = true;
            return true;    
        }else{
            return false;
        }
    }
    bool Join()
    {
        if(isrunning == false){
            return true;
        }
        int n = pthread_join(_tid, NULL);
        if(n == 0){
            isrunning = false;
            return true;
        }else return false;
    }
    std::string Threadname()
    {
        return _threadname;
    }
    bool isrun()
    {
        return isrunning;
    }
    ~Thread(){}
private:
    pthread_t _tid;
    std::string _threadname;
    func_t<T> _func;
    T _data;
    bool isrunning;
};